<?php

class LoginModel {
		
	private $m_facadeModel;
	private $m_loggedIn = "LoggedIn";
	
	/**
	 * @access public
	 * @param FacadeModel
	 * @return -
	 */
	public function __construct(facadeModel $a_facadeModel){
		$this->m_facadeModel = $a_facadeModel;
	}

	/**
	 * Public function to check if user is logged in or not
	 * @access Public
	 * @param -
	 * @return bool
	 */
	public function isUserLoggedIn() {
		//Check if logged in
		if (isset($_SESSION[$this -> m_loggedIn]) === true) {
			//its true, return it!
			return TRUE;
		}
		//else return false
		return FALSE;
	}

	/**
	 * @access public
	 * @param User object
	 * @return bool
	 */
	public function logIn($a_userName, $a_passWord) {
		//try to login, if ok, return true

			if ($this -> m_facadeModel -> logInMember($a_userName, $a_passWord) === true) {
				$_SESSION[$this -> m_loggedIn] = $a_userName;
				return true;
			}
		// Else return false
		else {
			messageModel::addMessage("<p>Felaktiga uppgifter!<p>");
			return false;
		}
	}

	/**
	 * Public Function to LOGOUT user
	 * @access public
	 * @param -
	 * @return Bool
	 */
	public function logOut() {
		if (isset($_SESSION[$this -> m_loggedIn])) {
			unset($_SESSION[$this -> m_loggedIn]);
			setcookie(session_name(), $_COOKIE[session_name()], time() - 10000);
			return true;
		}
		return false;
	}

}
